function X = cicaarmix(invA0,Atau,S,comps)
% CICAARMIX convolutive mixing
%
%   X = cicaarmix(invA0,Atau,S [,comps])
%   X = cicaarmix([],A,S [,comps])
%
% where invA0 is D-by-D; Atau is D-by-KC-by-L; S is k-by-N; 
% comps (optinal) specifies a list of components to forward.
%
% Copyright: Mads Dyrholm, 2007
if isempty(invA0)
  A0 = Atau(:,:,1);
  Atau = Atau(:,:,2:end);
else
  A0 = pinv(invA0);
end
if nargin<4, comps = 1:size(S,1); end
if isempty(Atau)
  X = A0(:,comps)*S(comps,:);
  return
end
[D,KC,L] = size(Atau);
[K,N] = size(S);
if KC>0
  Atau(:,KC+1:K,:)=0;
else
  Atau = zeros(D,D,L);
end 
X = zeros(D,L+N);
for d=1:D
  for k=1:K
    if isempty(find(comps==k)), continue, end
    X(d,:) = X(d,:) + conv(S(k,:),[A0(d,k) squeeze(Atau(d,k,:))']);
  end
end
X = X(:,1:N);
